//
//  MusicPlayViewController.m
//  DolphinMusic
//
//  Created by dllo on 15/3/28.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MusicPlayViewController.h"
#import "Singer.h"
#import "PlayView.h"
#import "SampleQueueId.h"
#import "MBProgressHUD.h"
#import "Song.h"
#import "SIngerViewController.h"
#import "ListDetailsTableViewCell.h"
#import "UIImageView+WebCache.h"

#import "STKLocalFileDataSource.h"

#import <ShareSDK/ShareSDK.h>
#import "WeiboApi.h"
#import "WeiboSDK.h"

#import "CDSideBarController.h"

@interface MusicPlayViewController () <STKAudioPlayerDelegate, UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate, CDSideBarControllerDelegate>
{
    CDSideBarController *sideBar;
}


@property (nonatomic, retain)UIImageView *coverImageView;
@property (nonatomic, retain)UIButton *backButton;
@property (nonatomic, retain)UILabel *titleLabel;
@property (nonatomic, retain)UILabel *authorLabel;

@property (nonatomic, retain)UIButton *collectButton;

@property (nonatomic, retain)NSString *singerID;
@property (nonatomic, retain)NSString *ting_uid;


@property (nonatomic, retain)UIButton *singerButton;
@property (nonatomic, retain)UIButton *lrcButton;
@property (nonatomic, retain)UIView *view1;
@property (nonatomic, retain)MBProgressHUD *HUD;



@property (nonatomic, retain)UISlider *processSlider;
@property (nonatomic, retain)UILabel *playedTimeLabel;
@property (nonatomic, retain)UILabel *allTimeLabel;
@property (nonatomic, retain)NSTimer *timer;
@property (nonatomic, retain)UIButton *shareButton;


@property (nonatomic, retain)UIButton *playButton;
@property (nonatomic, retain)UIButton *lastButton;
@property (nonatomic, retain)UIButton *nextButton;


// 动态歌词相关
@property (nonatomic, retain)NSString *lrcStr;
@property (nonatomic, retain)NSMutableDictionary *lrcDictionary;
@property (nonatomic, retain)NSMutableArray *timeArr;
@property (nonatomic, assign)NSInteger lrcLineNumber;
@property (nonatomic, retain)UITableView *tableView;


@property (nonatomic, assign)BOOL songIfCollect;
@property (nonatomic, assign)BOOL albumIfCollect;

@property (nonatomic, retain)UITableView *singerTableView;
@property (nonatomic, retain)NSMutableArray *singerSongArr;
@property (nonatomic, retain)UIImageView *singerImageView;
@property (nonatomic, retain)UILabel *singerNameLabel;
@property (nonatomic, retain)UILabel *albumNameLabel;


@property (nonatomic, retain)UIButton *playTypeButton;

@property (nonatomic, assign)BOOL haveLrc;
@property (nonatomic, retain)UITextView *textView;

@property (nonatomic, retain)NSString *localSongFile;
@property (nonatomic, assign)BOOL ifDownload;
@property (nonatomic, retain)NSString *playStr;


@end

@implementation MusicPlayViewController


+ (MusicPlayViewController *)shareInstance
{
    
    static MusicPlayViewController *musicPlayVC = nil;
    if (musicPlayVC == nil) {
        musicPlayVC = [[MusicPlayViewController alloc] init];
    }
    return musicPlayVC;
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    
    UIWindow *window = [[[UIApplication sharedApplication] windows] firstObject];
    UIView *playView = [window viewWithTag:10001];
    playView.hidden = NO;
    
}

- (void)viewWillAppear:(BOOL)animated
{
    UIWindow *window = [[[UIApplication sharedApplication] windows] firstObject];
    UIView *playView = [window viewWithTag:10001];
    playView.hidden = YES;
    
    
    UIButton *button = (UIButton *) [window viewWithTag:1000];
    button.hidden = YES;
    
    [self.singerTableView reloadData];
    
    
    self.songIfCollect = NO;
    DataBaseHandler *dbHandler = [DataBaseHandler shareInstance];
    
    NSMutableArray *array = [dbHandler selectAllInSongCollection];
    
    for (Song *song in array) {
        if ([song.song_id isEqualToString:self.songID]) {
            self.songIfCollect = YES;
        }
    }
    
    if (self.songIfCollect == NO) {
        [self.collectButton setBackgroundImage:[UIImage imageNamed:@"weishoucang.png"] forState:UIControlStateNormal];
    }
    
    else {
        [self.collectButton setBackgroundImage:[UIImage imageNamed:@"yishoucang.png"] forState:UIControlStateNormal];
    }
    
    
    self.ifDownload = NO;
    NSMutableArray *downLoadSongArr = [dbHandler selectAllInFilePath];
    for (Song *song in downLoadSongArr) {
        
        if ([self.songID isEqualToString:song.song_id]) {
            
            NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            
            NSString *filePath = [NSString stringWithFormat:@"%@/%@.mp3", cachePath, self.songID];
            
            self.localSongFile = filePath;
            self.ifDownload = YES;
        }
    }
    
    if (self.ifDownload == NO) {
        [self.downloadButton setBackgroundImage:[UIImage imageNamed:@"weixiazai.png"] forState:UIControlStateNormal];
    }
    else {
        [self.downloadButton setBackgroundImage:[UIImage imageNamed:@"yixiazai.png"] forState:UIControlStateNormal];
    }
    
}





- (void)track
{
    if (_processSlider) {
        _processSlider.maximumValue = self.player.duration;
        _processSlider.value = self.player.progress;
        
        NSInteger proMin = (NSInteger)self.player.progress / 60;
        NSInteger proSec = (NSInteger)self.player.progress % 60;
        
        NSInteger durMin = (NSInteger)self.player.duration / 60;
        NSInteger durSec = (NSInteger)self.player.duration % 60;
        
        self.playedTimeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld", proMin, proSec];
        
        self.allTimeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld", durMin, durSec];
        
        NSString *postStr = [NSString stringWithFormat:@"%@/%@", self.playedTimeLabel.text, self.allTimeLabel.text];
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center postNotificationName:@"更新时间" object:postStr];
        [self displaySondWord:self.player.progress];
    }
}






- (void)setSongID:(NSString *)songID
{
    if (_songID != songID) {
        [_songID release];
        _songID = [songID retain];
    }
    [self.HUD show:YES];
    [self.player stop];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIWindow *window = [[[UIApplication sharedApplication] windows] firstObject];
    UIView *playView = [window viewWithTag:10001];
    playView.userInteractionEnabled = YES;
    
    self.scrollView.contentOffset = CGPointMake(0, 0);
    
    if ([self.type isEqualToString:@"music"]) {
        [self setMusicPage];
    }
    
    if ([self.type isEqualToString:@"FM"])
    {
        [self setFMPage];
    }
}


- (void)setMusicPage
{
    self.playTypeButton.userInteractionEnabled = YES;
    
    if (self.playType == NO) {
        [self.playTypeButton setBackgroundImage:[UIImage imageNamed:@"shunxubofang.png"] forState:UIControlStateNormal];
    }
    else {
        [self.playTypeButton setBackgroundImage:[UIImage imageNamed:@"danquxunhuan.png"] forState:UIControlStateNormal];
    }

    
    self.scrollView.frame = CGRectMake(0, 80, VIEW_WIDTH, VIEW_HEIGHT - VIEW_HEIGHT * 80.0 / 667.0 - 15 - 80);
    
    self.songIfCollect = NO;
    DataBaseHandler *dbHandler = [DataBaseHandler shareInstance];
    
    NSMutableArray *array = [dbHandler selectAllInSongCollection];
    
    for (Song *song in array) {
        if ([song.song_id isEqualToString:self.songID]) {
            self.songIfCollect = YES;
        }
    }
    if (self.songIfCollect == NO) {
        [self.collectButton setBackgroundImage:[UIImage imageNamed:@"weishoucang.png"] forState:UIControlStateNormal];
    }
    else {
        [self.collectButton setBackgroundImage:[UIImage imageNamed:@"yishoucang.png"] forState:UIControlStateNormal];
    }
    
    self.scrollView.contentSize = CGSizeMake(VIEW_WIDTH * 3, 0);
    self.scrollView.contentOffset = CGPointMake(VIEW_WIDTH, 0);
    self.scrollView.userInteractionEnabled = YES;
    self.singerButton.hidden = NO;
    self.view1.hidden = NO;
    self.lrcButton.hidden = NO;
    self.shareButton.hidden = NO;
    self.downloadButton.hidden = NO;
    self.collectButton.hidden = NO;
    
    self.ifDownload = NO;
    
    NSMutableArray *downLoadSongArr = [dbHandler selectAllInFilePath];
    for (Song *song in downLoadSongArr) {
        
        if ([self.songID isEqualToString:song.song_id]) {
            
            NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            
            NSString *filePath = [NSString stringWithFormat:@"%@/%@.mp3", cachePath, self.songID];
            
            self.localSongFile = filePath;
            self.ifDownload = YES;
        }
    }
    
    self.address = [NSString stringWithFormat:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=webapp_music&method=baidu.ting.song.play&format=json&callback=&songid=%@&_=1413017198449", self.songID];
    
    
    if (self.ifDownload == NO) {
        [NetHandler getDataWithUrl:self.address completion:^(id dict) {
            
            [self.downloadButton setBackgroundImage:[UIImage imageNamed:@"weixiazai.png"] forState:UIControlStateNormal];

            NSDictionary *dic = [dict objectForKey:@"songinfo"];
            self.singerID = [dic objectForKey:@"artist_id"];
            self.ting_uid = [dic objectForKey:@"ting_uid"];
            self.songTitle = [dic objectForKey:@"title"];
            self.singerName = [dic objectForKey:@"author"];
            self.imageStr = [dic objectForKey:@"pic_premium"];
            NSString *albumName = [dic objectForKey:@"album_title"];
            
            
            [self.timeArr removeAllObjects];
            [self.lrcDictionary removeAllObjects];
            NSString *lrcAddress = [dic objectForKey:@"lrclink"];
            NSError *error1 = nil;
            self.lrcStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:[lrcAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] encoding:NSUTF8StringEncoding error:&error1];
            
            self.textView.text = nil;
            if (self.lrcStr == nil) {
                self.textView.text = @"暂无歌词";
                [self.tableView reloadData];
            }
            else if ([self.lrcStr hasPrefix:@"\n["] || [self.lrcStr hasPrefix:@"["]) {
                self.textView.text = nil;
                [self initLrc];
                [self.tableView reloadData];
                
            }
            else {
                self.lrcStr = [@"该歌词不支持实时滚动\n\n" stringByAppendingString:self.lrcStr];
                self.textView.text = self.lrcStr;
                [self.tableView reloadData];
            }
            
            
            [self.singerImageView setImageWithURL:[NSURL URLWithString:[self.imageStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
            self.singerNameLabel.text = [NSString stringWithFormat:@"歌手: %@", self.singerName];
            self.singerNameLabel.textColor = [UIColor whiteColor];
            self.albumNameLabel.text = [NSString stringWithFormat:@"专辑:  %@", albumName];
            self.albumNameLabel.textColor = [UIColor whiteColor];
            
            NSString *nextPageAddress = [NSString stringWithFormat:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.0.1&method=baidu.ting.artist.getSongList&format=json&order=2&tinguid=%@&artistid=%@&offset=0&limits=50", self.ting_uid, self.singerID];
            
            [NetHandler getDataWithUrl:nextPageAddress completion:^(id dict) {
                [self.singerSongArr removeAllObjects];
                
                NSArray *songarrr = [dict objectForKey:@"songlist"];
                if ([songarrr isKindOfClass:[NSArray class]]) {
                    for (NSDictionary *dic in songarrr) {
                        Song *song = [[Song alloc] initWithDictionary:dic];
                        [self.singerSongArr addObject:song];
                        [song release];
                    }
                }
                [self.singerTableView reloadData];
            }];
            
            NSString *imageStr = [[dict objectForKey:@"songinfo"] objectForKey:@"pic_premium"];
            imageStr = [imageStr stringByRemovingPercentEncoding];
            
            [self.coverImageView setImageWithURL:[NSURL URLWithString:[imageStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
            [self.view bringSubviewToFront:self.backButton];
            
            
            self.titleLabel.frame = CGRectMake(50, 30, VIEW_WIDTH - 150, 20);
            self.titleLabel.font = [UIFont systemFontOfSize:17];
            self.authorLabel.frame = CGRectMake(50, 55, VIEW_WIDTH - 150, 15);
            self.authorLabel.font = [UIFont systemFontOfSize:13];
            
            
            self.titleLabel.text = [[dict objectForKey:@"songinfo"] objectForKey:@"title"];
            self.authorLabel.text = [[dict objectForKey:@"songinfo"] objectForKey:@"author"];
            
            self.song.title = self.titleLabel.text;
            self.song.author = self.authorLabel.text;
            self.song.song_id = self.songID;
            
            NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
            
            NSArray *arr11 = @[self.titleLabel.text, self.singerName, self.imageStr];
            [center postNotificationName:@"歌曲信息" object:arr11];
            [center postNotificationName:@"播放按钮" object:nil];
            
            
            DataBaseHandler *dbHandler = [DataBaseHandler shareInstance];
            
            NSMutableArray *recentPlayArr = [dbHandler selectAllInRecentPlay];
            for (Song *song in recentPlayArr) {
                if ([self.song.song_id isEqualToString:song.song_id]) {
                    [dbHandler deleteRecentPlayWithSongID:self.song.song_id];
                }
            }
            [dbHandler insertRecentPlay:self.song];
            
            //  播放歌曲
            
            
            self.playStr = [[dict objectForKey:@"bitrate"] objectForKey:@"file_link"];
            if (self.playStr != nil) {
                [self.player play:self.playStr];
                
                
            }
            else {
                UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"缺少资源, 客官换首歌吧~" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alterView show];
                [alterView release];
            }
            
            
            self.timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(track) userInfo:nil repeats:YES];
            [self.timer fire];
            [self.HUD hide:YES];
        }];
    }
    else {
        
        [self.downloadButton setBackgroundImage:[UIImage imageNamed:@"yixiazai.png"] forState:UIControlStateNormal];
        
        [self.tableView reloadData];
        self.textView.text = @"";
        
        
        NSURL *localSongFilePath = [NSURL fileURLWithPath:self.localSongFile];
        
        [self.player playURL:localSongFilePath];
        
        self.titleLabel.frame = CGRectMake(50, 30, VIEW_WIDTH - 150, 20);
        self.titleLabel.font = [UIFont systemFontOfSize:17];
        self.authorLabel.frame = CGRectMake(50, 55, VIEW_WIDTH - 150, 15);
        self.authorLabel.font = [UIFont systemFontOfSize:13];
        
        
        self.titleLabel.text = self.songTitle;
        self.authorLabel.text = self.singerName;
        
        self.song.title = self.titleLabel.text;
        self.song.author = self.authorLabel.text;
        self.song.song_id = self.songID;
        
        
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        
        NSArray *arr11 = @[self.titleLabel.text, self.singerName, @"what"];
        [center postNotificationName:@"歌曲信息" object:arr11];
        [center postNotificationName:@"播放按钮" object:nil];
        
        
        DataBaseHandler *dbHandler = [DataBaseHandler shareInstance];
        
        NSMutableArray *recentPlayArr = [dbHandler selectAllInRecentPlay];
        for (Song *song in recentPlayArr) {
            if ([self.song.song_id isEqualToString:song.song_id]) {
                [dbHandler deleteRecentPlayWithSongID:self.song.song_id];
            }
        }
        [dbHandler insertRecentPlay:self.song];
        
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(track) userInfo:nil repeats:YES];
        [self.timer fire];
        [self.HUD hide:YES];
        
        [NetHandler getDataWithUrl:self.address completion:^(id dict) {
            
                    NSDictionary *dic = [dict objectForKey:@"songinfo"];
                    self.singerID = [dic objectForKey:@"artist_id"];
                    self.ting_uid = [dic objectForKey:@"ting_uid"];
                    self.singerName = [dic objectForKey:@"author"];
                    self.imageStr = [dic objectForKey:@"pic_premium"];
                    NSString *albumName = [dic objectForKey:@"album_title"];
            
            NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
            
            NSArray *arr11 = @[self.songTitle, self.singerName, self.imageStr];
            [center postNotificationName:@"歌曲信息" object:arr11];
            [center postNotificationName:@"播放按钮" object:nil];
            
            
            NSString *imageStr = [[dict objectForKey:@"songinfo"] objectForKey:@"pic_premium"];
            imageStr = [imageStr stringByRemovingPercentEncoding];
            
            [self.coverImageView setImageWithURL:[NSURL URLWithString:[imageStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
            [self.view bringSubviewToFront:self.backButton];
            
            
                    [self.timeArr removeAllObjects];
                    [self.lrcDictionary removeAllObjects];
                    NSString *lrcAddress = [dic objectForKey:@"lrclink"];
                    NSError *error1 = nil;
                    self.lrcStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:[lrcAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] encoding:NSUTF8StringEncoding error:&error1];
            
                    self.textView.text = nil;
                    if (self.lrcStr == nil) {
                        self.textView.text = @"暂无歌词";
                        [self.tableView reloadData];
                    }
                    else if ([self.lrcStr hasPrefix:@"\n["] || [self.lrcStr hasPrefix:@"["]) {
                        self.textView.text = nil;
                        [self initLrc];
                        [self.tableView reloadData];
            
                    }
                    else {
                        self.lrcStr = [@"该歌词不支持实时滚动\n\n" stringByAppendingString:self.lrcStr];
                        self.textView.text = self.lrcStr;
                        [self.tableView reloadData];
                    }
            
            
                    [self.singerImageView setImageWithURL:[NSURL URLWithString:[self.imageStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
                    self.singerNameLabel.text = [NSString stringWithFormat:@"歌手: %@", self.singerName];
                    self.singerNameLabel.textColor = [UIColor whiteColor];
                    self.albumNameLabel.text = [NSString stringWithFormat:@"专辑:  %@", albumName];
                    self.albumNameLabel.textColor = [UIColor whiteColor];
            
                    NSString *nextPageAddress = [NSString stringWithFormat:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.0.1&method=baidu.ting.artist.getSongList&format=json&order=2&tinguid=%@&artistid=%@&offset=0&limits=50", self.ting_uid, self.singerID];
            
                    [NetHandler getDataWithUrl:nextPageAddress completion:^(id dict) {
                        [self.singerSongArr removeAllObjects];
            
                        NSArray *songarrr = [dict objectForKey:@"songlist"];
                        if ([songarrr isKindOfClass:[NSArray class]]) {
                            for (NSDictionary *dic in songarrr) {
                                Song *song = [[Song alloc] initWithDictionary:dic];
                                [self.singerSongArr addObject:song];
                                [song release];
                            }
                        }
                        [self.singerTableView reloadData];
                    }];
            
        }];
        
    }
}

- (void)downloadButtonAction:(UIButton *)button
{
    if (self.ifDownload == YES) {
        
    }
    else {
        DataBaseHandler *dbHandler = [DataBaseHandler shareInstance];
        [dbHandler downLoadSongWithSongID:self.songID];
        self.ifDownload = YES;
    }
}


- (void)setFMPage
{
    self.playTypeButton.userInteractionEnabled = NO;
    self.singerButton.hidden = YES;
    self.view1.hidden = YES;
    self.lrcButton.hidden = YES;
    self.shareButton.hidden  = YES;
    self.downloadButton.hidden = YES;
    self.collectButton.hidden = YES;
    self.scrollView.contentOffset = CGPointMake(VIEW_WIDTH, 0);
    self.scrollView.userInteractionEnabled = NO;
    [self.backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [NetHandler getDataWithUrl:self.address completion:^(id dict) {
        
        self.singerName = [dict objectForKey:@"albumTitle"];
        self.imageStr = [dict objectForKey:@"coverLarge"];
        
        self.imageStr = [self.imageStr stringByRemovingPercentEncoding];
        
        [self.coverImageView setImageWithURL:[NSURL URLWithString:[self.imageStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
        [self.view bringSubviewToFront:self.backButton];
        
        self.titleLabel.text = self.singerName;
        self.titleLabel.frame = CGRectMake(50, 30, VIEW_WIDTH - 70, 20);
        self.titleLabel.font = [UIFont systemFontOfSize:16];
        
        self.authorLabel.text = [dict objectForKey:@"title"];
        self.authorLabel.frame = CGRectMake(50, 55, VIEW_WIDTH - 70, 15);
        self.authorLabel.font = [UIFont systemFontOfSize:12];
        self.authorLabel.numberOfLines = 0;
        [self.authorLabel sizeToFit];
        
        self.scrollView.frame = CGRectMake(0, self.authorLabel.frame.origin.y + self.authorLabel.frame.size.height + 10, VIEW_WIDTH, VIEW_HEIGHT - VIEW_HEIGHT * 80.0 / 667.0 - 15 - 80);
        
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        NSArray *arr11 = @[self.titleLabel.text, self.singerName, self.imageStr];
        [center postNotificationName:@"歌曲信息" object:arr11];
        [center postNotificationName:@"播放按钮" object:nil];
        
        
        //  播放歌曲
        NSString *playStr = [dict objectForKey:@"playUrl64"];
        if (playStr != nil) {
           [self.player play:playStr];
        }
        else {
            UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"缺少资源, 客官换首歌吧~" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alterView show];
            [alterView release];
        }
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(track) userInfo:nil repeats:YES];
    }];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.song = [[Song alloc] init];
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(lastButtonAction:) name:@"上一曲" object:nil];
    [center addObserver:self selector:@selector(nextButtonAction:) name:@"下一曲" object:nil];
    [center addObserver:self selector:@selector(nextButtonAction:) name:@"viewControllerEmotion" object:nil];
    
    self.player = [[STKAudioPlayer alloc] initWithOptions:(STKAudioPlayerOptions){ .flushQueueOnSeek = YES, .enableVolumeMixer = YES, .equalizerBandFrequencies = {50, 100, 200, 400, 800, 1600, 2600, 16000} }];
    
    
    self.player.delegate = self;
    self.player.volume = 0.6;
    self.player.meteringEnabled = YES;
    
    
    UIImageView *backView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, VIEW_WIDTH, VIEW_HEIGHT - 20)];
    backView.image = [UIImage imageNamed:@"playBackView.jpg"];
    [self.view addSubview:backView];
    [backView release];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 80, VIEW_WIDTH, VIEW_HEIGHT - VIEW_HEIGHT * 80.0 / 667.0 - 15 - 80)];
    self.scrollView.contentSize = CGSizeMake(VIEW_HEIGHT * 3, 0);
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    
    self.HUD = [[MBProgressHUD alloc] initWithView:self.scrollView];
    [self.scrollView addSubview:self.HUD];
    self.HUD.labelText = @"客官请稍等...";
    self.HUD.dimBackground = YES;
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 30, VIEW_WIDTH - 150, 20)];
    self.titleLabel.font = [UIFont systemFontOfSize:17];
    self.titleLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:self.titleLabel];
    [_titleLabel release];
    
    self.authorLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 55, VIEW_WIDTH - 150, 15)];
    self.authorLabel.font = [UIFont systemFontOfSize:13];
    self.authorLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:self.authorLabel];
    [_authorLabel release];
    
    
    self.coverImageView = [[UIImageView alloc] initWithFrame:CGRectMake(VIEW_WIDTH + 10, 0, VIEW_WIDTH - 20, 400.0 / 667 * VIEW_HEIGHT)];
    self.coverImageView.layer.cornerRadius = 10;
    self.coverImageView.clipsToBounds = YES;
    [self.scrollView addSubview:self.coverImageView];
    
    self.downloadButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.downloadButton.frame = CGRectMake(VIEW_WIDTH + VIEW_WIDTH / 4 - 10, self.coverImageView.frame.size.height + 15, 20, 20);
    [self.downloadButton setBackgroundImage:[UIImage imageNamed:@"weixiazai.png"] forState:UIControlStateNormal];
    [self.scrollView addSubview:self.downloadButton];
    [self.downloadButton addTarget:self action:@selector(downloadButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.collectButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.collectButton.frame = CGRectMake(VIEW_WIDTH + VIEW_WIDTH / 2 - 10, self.downloadButton.frame.origin.y, 20, 20);
    [self.collectButton setBackgroundImage:[UIImage imageNamed:@"weishoucang.png"] forState:UIControlStateNormal];
    [self.collectButton addTarget:self action:@selector(collectButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:self.collectButton];

    self.shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.shareButton.frame = CGRectMake(VIEW_WIDTH + 3 * VIEW_WIDTH / 4 - 10, self.collectButton.frame.origin.y, 20, 20);
    [self.shareButton setBackgroundImage:[UIImage imageNamed:@"share.png"] forState:UIControlStateNormal];
    [self.shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:self.shareButton];
    
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 20)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:whiteView];
    [whiteView release];
    
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.backButton.frame = CGRectMake(0, 20, 50, 44);
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 20, 20)];
    imageView.image = [UIImage imageNamed:@"backButton.png"];
    [self.backButton addSubview:imageView];
    [imageView release];
    
//    [self.backButton setBackgroundImage:[UIImage imageNamed:@"backButton.png"] forState:UIControlStateNormal];
    [self.backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.backButton];
    
    
    self.singerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.singerButton.frame = CGRectMake(VIEW_WIDTH - 90, 40, 40, 20);
    [self.singerButton setTitle:@"歌手" forState:UIControlStateNormal];
    [self.singerButton addTarget:self action:@selector(lrcButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.singerButton.tintColor = [UIColor whiteColor];
    self.singerButton.hidden = YES;
    [self.view addSubview:self.singerButton];
    
    self.view1 = [[UIView alloc] initWithFrame:CGRectMake(VIEW_WIDTH - 48, 35, 2, 32)];
    self.view1.hidden = YES;
    self.view1.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.view1];
    [self.view1 release];
    
    self.lrcButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.lrcButton.frame = CGRectMake(VIEW_WIDTH - 45, 40, 40, 20);
    [self.lrcButton setTitle:@"歌词" forState:UIControlStateNormal];
    self.lrcButton.tintColor = [UIColor whiteColor];
    self.lrcButton.hidden = YES;
    [self.lrcButton addTarget:self action:@selector(lrcButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.lrcButton];

    UIView *view11 = [[UIView alloc] initWithFrame:CGRectMake(0, VIEW_HEIGHT - (VIEW_HEIGHT * 80.0 / 667.0), VIEW_WIDTH, VIEW_HEIGHT * 80.0 / 667.0)];
    view11.backgroundColor = [UIColor whiteColor];
    view11.alpha = 0.15;
    [self.view addSubview:view11];
    [view11 release];
    
    self.playTypeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.playTypeButton.frame = CGRectMake(30.0 / 375 * VIEW_WIDTH, view11.frame.origin.y + 40.0 / 667 * VIEW_WIDTH, 30, 30);
    [self.playTypeButton setBackgroundImage:[UIImage imageNamed:@"shunxubofang.png"] forState:UIControlStateNormal];
    
    
    [self.playTypeButton addTarget:self action:@selector(playTyprButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.playTypeButton];
    
    self.lastButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.lastButton.frame = CGRectMake(90.0 / 375 * VIEW_WIDTH , view11.frame.origin.y + 30.0 / 667 * VIEW_WIDTH , 40, 40);
    [self.lastButton setBackgroundImage:[UIImage imageNamed:@"aboveMusic.png"] forState:UIControlStateNormal];
    [self.lastButton addTarget:self action:@selector(lastButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.lastButton];
    
    
    self.playButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.playButton.frame = CGRectMake(160.0 / 375 * VIEW_WIDTH , view11.frame.origin.y + 30.0 / 667 * VIEW_WIDTH , 51, 40);
    [self.playButton setBackgroundImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
    [self.playButton addTarget:self action:@selector(playButtonAction1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.playButton];

    self.nextButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.nextButton.frame = CGRectMake(285.0 / 375 * VIEW_WIDTH - 40, view11.frame.origin.y + 30.0 / 667 * VIEW_WIDTH , 40, 40);
    [self.nextButton setBackgroundImage:[UIImage imageNamed:@"nextMusic.png"] forState:UIControlStateNormal];
    [self.nextButton addTarget:self action:@selector(nextButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.nextButton];
    
    
    
    self.processSlider = [[UISlider alloc] initWithFrame:CGRectMake(-30, view11.frame.origin.y - 5, VIEW_WIDTH + 60, 10)];
    [self.view addSubview:self.processSlider];
    [self.processSlider setThumbImage:[UIImage imageNamed:@"soundSlider.png"] forState:UIControlStateNormal];
    [self.processSlider addTarget:self action:@selector(seekSlider:) forControlEvents:UIControlEventValueChanged];
    [_processSlider release];
    
    
    self.playedTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, view11.frame.origin.y + 5, 50, 20)];
    self.playedTimeLabel.textColor = [UIColor whiteColor];
    self.playedTimeLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:self.playedTimeLabel];
    
    self.allTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(VIEW_WIDTH - 42, view11.frame.origin.y + 5, 42, 20)];
    self.allTimeLabel.textColor = [UIColor whiteColor];
    self.allTimeLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:self.allTimeLabel];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(VIEW_WIDTH * 2 + 20, 45, VIEW_WIDTH - 40, 385.0 / 667 * VIEW_HEIGHT) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.userInteractionEnabled = NO;
    self.tableView.rowHeight = 35.0 / 667 * VIEW_HEIGHT;
    self.tableView.separatorColor = [UIColor clearColor];
    [self.scrollView addSubview:self.tableView];
    [_tableView release];
    
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(VIEW_WIDTH * 2 + 20, 45, VIEW_WIDTH - 40, 385.0 / 667 * VIEW_HEIGHT)];
    self.textView.textColor = [UIColor whiteColor];
    self.textView.font = [UIFont systemFontOfSize:13];
    self.textView.textAlignment = NSTextAlignmentCenter;
    self.textView.backgroundColor = [UIColor clearColor];
    self.textView.editable = NO;
    [self.scrollView addSubview:self.textView];
    [_textView release];
    
    
    self.lrcDictionary = [[NSMutableDictionary alloc] initWithCapacity:11];
    self.timeArr = [[NSMutableArray alloc] initWithCapacity:11];
    
    
    self.singerSongArr = [NSMutableArray array];
    self.singerTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH - 10, self.scrollView.frame.size.height) style:UITableViewStylePlain];
    self.singerTableView.delegate = self;
    self.singerTableView.dataSource = self;
    self.singerTableView.rowHeight = 60;
    self.singerTableView.showsVerticalScrollIndicator = NO;
    self.singerTableView.backgroundColor = [UIColor clearColor];
    [self.singerTableView registerClass:[ListDetailsTableViewCell class] forCellReuseIdentifier:@"singersongTableViewreuseaa"];
    
    
    UIView *viewSingerHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 70)];
    viewSingerHeader.backgroundColor = [UIColor clearColor];
    self.singerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 60, 60)];
    [viewSingerHeader addSubview:self.singerImageView];
    [_singerImageView release];
    
    self.singerNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(75, 5, self.singerTableView.frame.size.width - 85, 30)];
    self.singerNameLabel.font = [UIFont systemFontOfSize:15];
    self.singerNameLabel.numberOfLines = 0;
    [viewSingerHeader addSubview:self.singerNameLabel];
    [_singerNameLabel release];
    
    self.albumNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(75, 35, self.singerTableView.frame.size.width - 85, 30)];
    self.albumNameLabel.font = [UIFont systemFontOfSize:15];
    self.albumNameLabel.numberOfLines = 0;
    [viewSingerHeader addSubview:self.albumNameLabel];
    [_albumNameLabel release];
    
    
    self.singerTableView.tableHeaderView = viewSingerHeader;
    [self.scrollView addSubview:self.singerTableView];
    
    [viewSingerHeader release];
    [_singerTableView release];
    
}






- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.scrollView) {
        
        if (scrollView.contentOffset.x <= VIEW_WIDTH / 2) {
            [self.singerButton setTitle:@"图片" forState:UIControlStateNormal];
            [self.lrcButton setTitle:@"歌词" forState:UIControlStateNormal];
        }
        else if (scrollView.contentOffset.x > VIEW_WIDTH / 2 && scrollView.contentOffset.x <= VIEW_WIDTH / 2 * 3)
        {
            [self.singerButton setTitle:@"歌手" forState:UIControlStateNormal];
            [self.lrcButton setTitle:@"歌词" forState:UIControlStateNormal];
        }
        else {
            [self.singerButton setTitle:@"歌手" forState:UIControlStateNormal];
            [self.lrcButton setTitle:@"图片" forState:UIControlStateNormal];
        }
        
    }
}


#pragma mark - 各种button方法

- (void)playTyprButton:(UIButton *)button
{
    self.playType = !self.playType;
    if (self.playType == NO) {
        [self.playTypeButton setBackgroundImage:[UIImage imageNamed:@"shunxubofang.png"] forState:UIControlStateNormal];
    }
    else {
        [self.playTypeButton setBackgroundImage:[UIImage imageNamed:@"danquxunhuan.png"] forState:UIControlStateNormal];
    }
}

- (void)shareButtonAction:(UIButton *)button
{
    //1.定制分享的内容
    
    
    NSString *str1 = [NSString stringWithFormat:@"我正在使用海豚音乐盒, 分享一首特别好听的歌曲: %@  %@ %@ @有-何不可", self.songTitle, self.singerName, self.playStr];
    
    id<ISSContent> publishContent = [ShareSDK content:str1 defaultContent:nil image:[ShareSDK imageWithUrl:self.imageStr] title:@"好听的歌曲" url:nil description:@"This is brief introduction" mediaType:SSPublishContentMediaTypeImage];
    //2.调用分享菜单分享
    [ShareSDK showShareActionSheet:nil shareList:nil content:publishContent statusBarTips:YES authOptions:nil shareOptions:nil result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
        
        //如果分享成功
        if (state == SSResponseStateSuccess) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"分享成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
        
        //如果分享失败
        if (state == SSResponseStateFail) {
            NSLog(@"分享失败,错误码:%ld,错误描述%@",(long)[error errorCode],[error errorDescription]);
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Warning" message:@"分享失败" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
        
        
    }];

}

- (void)backButtonAction:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

- (void)collectButtonAction:(UIButton *)button
{
    DataBaseHandler *dbHandler = [DataBaseHandler shareInstance];
    if (self.songIfCollect == NO) {
        
        [dbHandler insertSong:self.song];
        
        [self.collectButton setBackgroundImage:[UIImage imageNamed:@"yishoucang.png"] forState:UIControlStateNormal];
    }
    else {
        [dbHandler deleteSong:self.song];
        
        [self.collectButton setBackgroundImage:[UIImage imageNamed:@"weishoucang.png"] forState:UIControlStateNormal];
    }
    self.songIfCollect = !self.songIfCollect;
}

- (void)lrcButtonAction:(UIButton *)button
{
    if ([button.titleLabel.text isEqualToString:@"歌词"]) {
        self.scrollView.contentOffset = CGPointMake(VIEW_WIDTH * 2, 0);
    }
    if ([button.titleLabel.text isEqualToString:@"图片"]) {
        self.scrollView.contentOffset = CGPointMake(VIEW_WIDTH, 0);
    }
    if ([button.titleLabel.text isEqualToString:@"歌手"]) {
        self.scrollView.contentOffset = CGPointMake(0, 0);
    }
}


#pragma mark - tableView协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
    return self.timeArr.count;
    }
    else if (tableView == self.singerTableView) {
        return self.singerSongArr.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == self.tableView) {
    
    static NSString *cellIdentifier = @"LRCCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.backgroundColor = [UIColor clearColor];
    if (indexPath.row == self.lrcLineNumber) {
        cell.textLabel.text = self.lrcDictionary[self.timeArr[indexPath.row]];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont systemFontOfSize:16];
        
        NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:self.lrcLineNumber inSection:0];
        [self.tableView selectRowAtIndexPath:indexPath1 animated:NO scrollPosition:UITableViewScrollPositionMiddle];
    } else {
        cell.textLabel.text = self.lrcDictionary[self.timeArr[indexPath.row]];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont systemFontOfSize:13];
    }
    cell.textLabel.backgroundColor = [UIColor clearColor];
    
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    
    return cell;
    }
    
    else if (tableView == self.singerTableView) {
        
        ListDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"singersongTableViewreuseaa" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor clearColor];
        cell.titleLabel.textColor = [UIColor whiteColor];
        cell.authorLabel.textColor = [UIColor whiteColor];
        cell.song = [self.singerSongArr objectAtIndex:indexPath.row];
        return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.singerTableView) {
        
        Song *song = [self.singerSongArr objectAtIndex:indexPath.row];
        self.playingNumber = indexPath.row;
        [self.playSongArr removeAllObjects];
        for (Song *song in self.singerSongArr) {
            NSString *songID = song.song_id;
            [self.playSongArr addObject:songID];
        }
        self.type = @"music";
        self.songID = song.song_id;
        self.scrollView.contentOffset = CGPointMake(VIEW_WIDTH, 0);
    }
    
    
}


#pragma mark 动态更新歌词表歌词

- (void)initLrc
{
    NSString *contentStr = self.lrcStr;
    NSArray *array = [contentStr componentsSeparatedByString:@"\n"];
    
    for (int i = 0; i < [array count]; i++) {
        NSString *linStr = [array objectAtIndex:i];
        NSArray *lineArray = [linStr componentsSeparatedByString:@"]"];
        if ([lineArray[0] length] > 8) {
            NSString *str1 = [linStr substringWithRange:NSMakeRange(3, 1)];
            NSString *str2 = [linStr substringWithRange:NSMakeRange(6, 1)];
            if ([str1 isEqualToString:@":"] && [str2 isEqualToString:@"."]) {
                NSString *lrcStr = [lineArray objectAtIndex:1];
                NSString *timeStr = [[lineArray objectAtIndex:0] substringWithRange:NSMakeRange(1, 5)];
                //分割区间求歌词时间
                //把时间 和 歌词 加入词典
                [self.lrcDictionary setObject:lrcStr forKey:timeStr];
                [self.timeArr addObject:timeStr];
                //timeArray的count就是行数
            }
        }
    }
}

- (void)displaySondWord:(double)time {
    
    for (int i = 0; i < [self.timeArr count]; i++) {
        
        NSArray *array = [self.timeArr[i] componentsSeparatedByString:@":"];//把时间转换成秒
        NSUInteger currentTime = [array[0] intValue] * 60 + [array[1] intValue];
        if (i == [self.timeArr count] - 1) {
            //求最后一句歌词的时间点
            NSArray *array1 = [self.timeArr[self.timeArr.count-1] componentsSeparatedByString:@":"];
            NSUInteger currentTime1 = [array1[0] intValue] * 60 + [array1[1] intValue];
            if (time > currentTime1) {
                [self updateLrcTableView:i];
                break;
            }
        } else {
            //求出第一句的时间点，在第一句显示前的时间内一直加载第一句
            NSArray *array2 = [self.timeArr[0] componentsSeparatedByString:@":"];
            NSUInteger currentTime2 = [array2[0] intValue] * 60 + [array2[1] intValue];
            if (time < currentTime2) {
                [self updateLrcTableView:0];
                break;
            }
            //求出下一步的歌词时间点，然后计算区间
            NSArray *array3 = [self.timeArr[i + 1] componentsSeparatedByString:@":"];
            NSUInteger currentTime3 = [array3[0] intValue] * 60 + [array3[1] intValue];
            if (time >= currentTime && time <= currentTime3) {
                [self updateLrcTableView:i];
                break;
            }
        }
    }
}

- (void)updateLrcTableView:(NSUInteger)lineNumber {
    self.lrcLineNumber = lineNumber;
    [self.tableView reloadData];
}


#pragma mark - 播放器的按钮进度条等方法

- (void)seekSlider:(UISlider *)slider
{
    if (_player) {
        [self.player seekToTime:slider.value];
    }
}

- (void)nextButtonAction:(UIButton *)button
{
    if (self.playingNumber != self.playSongArr.count - 1)
    {
        [self.player stop];
        
        self.playingNumber = self.playingNumber + 1;
        self.songID = [self.playSongArr objectAtIndex:self.playingNumber];
    }
}

- (void)lastButtonAction:(UIButton *)button
{
    
    if (self.playingNumber != 0) {
        
        [self.player stop];
        self.playingNumber = self.playingNumber - 1;
        self.songID = [self.playSongArr objectAtIndex:self.playingNumber];
    }
}

- (void)playButtonAction1:(UIButton *)button
{
    if (_player.state == STKAudioPlayerStatePlaying) {
        [button setBackgroundImage:[UIImage imageNamed:@"play1.png"] forState:UIControlStateNormal];
        [_player pause];
    }
    else if (_player.state == STKAudioPlayerStatePaused) {
        [button setBackgroundImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
        [_player resume];
    }
}


#pragma mark - 播放器协议方法
/// Raised when an item has started playing
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer didStartPlayingQueueItemId:(NSObject*)queueItemId
{
//    if ([self.type isEqualToString:@"music"]) {
//        
//        self.lrcDictionary = [[NSMutableDictionary alloc] initWithCapacity:11];
//        self.timeArr = [[NSMutableArray alloc] initWithCapacity:11];
//        
//    NSString *lrcUrl = [NSString stringWithFormat:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=webapp_music&method=baidu.ting.song.lry&format=json&callback=&songid=%@&_=1413017198449", self.songID];
//        [NetHandler getDataWithUrl:lrcUrl completion:^(id dict1) {
//            self.lrcStr = [dict1 objectForKey:@"lrcContent"];
//            [self initLrc];
//            [self.tableView reloadData];
//        }];
//}
}
/// Raised when an item has finished buffering (may or may not be the currently playing item)
/// This event may be raised multiple times for the same item if seek is invoked on the player
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer didFinishBufferingSourceWithQueueItemId:(NSObject*)queueItemId
{
//    NSLog(@"完成加载");
}
/// Raised when the state of the player has changed
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer stateChanged:(STKAudioPlayerState)state previousState:(STKAudioPlayerState)previousState
{
//    NSLog(@"播放状态改变");
}
/// Raised when an item has finished playing
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer didFinishPlayingQueueItemId:(NSObject*)queueItemId withReason:(STKAudioPlayerStopReason)stopReason andProgress:(double)progress andDuration:(double)duration
{
    if ([self.type isEqualToString:@"music"]) {
    
    if (stopReason != 2) {
        
        if (self.playType == NO) {
        [self.timer invalidate];
        [self.player stop];
        if (self.playingNumber != self.playSongArr.count - 1) {
        self.playingNumber = self.playingNumber + 1;
        self.songID = [self.playSongArr objectAtIndex:self.playingNumber];
        }
        }
        else if (self.playType == YES) {
            [self.timer invalidate];
            [self.player stop];
                self.songID = [self.playSongArr objectAtIndex:self.playingNumber];
        }
    }
    }
    
//    NSLog(@"结束播放");
    NSLog(@"结束原因: %d, progress: %f, duration: %f", stopReason, progress, duration);
}
/// Raised when an unexpected and possibly unrecoverable error has occured (usually best to recreate the STKAudioPlauyer)
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer unexpectedError:(STKAudioPlayerErrorCode)errorCode
{
//    NSLog(@"错误原因: %d", errorCode);
}


/// Optionally implemented to get logging information from the STKAudioPlayer (used internally for debugging)
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer logInfo:(NSString*)line
{
 //   NSLog(@"信息: %@", line);
}
/// Raised when items queued items are cleared (usually because of a call to play, setDataSource or stop)
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer didCancelQueuedItems:(NSArray*)queuedItems
{
//    NSLog(@"未知");
}


#pragma mark - 除掉边缘滑动返回
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
    
  
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

//- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
//{
//    NSLog(@"开始摇动");
//}
//
//- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
//{
//    NSLog(@"结束摇动");
//    [self nextButtonAction:self.nextButton];
//    
//}
//
//- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
//{
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
